# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re

class Untitled2(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(10)
        self.base_url = "http://localhost/php4dvd/"
        self.verificationErrors = []
        self.accept_next_alert = True
    
    def test_untitled2(self):
        driver = self.driver
        driver.get(self.base_url)

        #Вход в систему
        user = driver.find_element_by_id("username")
        user.clear()
        user.send_keys("admin")
        pas = driver.find_element_by_name("password")
        pas.clear()
        pas.send_keys("admin")
        driver.find_element_by_name("submit").click()

        #Первый поиск фильма и подсчет элементов
        search = driver.find_element_by_id("q")
        search.clear()
        search.send_keys("film1")
        search.send_keys(Keys.RETURN)
        count = len(driver.find_elements_by_xpath("//div[@title='film1' and @alt='film1']"))

        #Добавление нового фильма
        driver.find_element_by_css_selector("img[alt=\"Add movie\"]").click()
        name = driver.find_element_by_name("name")
        name.clear()
        name.send_keys("film1")
        year = driver.find_element_by_name("year")
        year.clear()
        year.send_keys("2012")
        driver.find_element_by_css_selector("img[alt=\"Save\"]").click()

        #Переход на главную
        driver.find_element_by_link_text("Home").click()

        #Поиск добавленного фильма
        search = driver.find_element_by_id("q")
        search.clear()
        search.send_keys("film1")
        search.send_keys(Keys.RETURN)

        #Ожидание загрузки элементов на странице и анализ результата
        for i in range(2):
            try:
                driver.find_element_by_id("results")
                break
            except:
                time.sleep(1)
                pass
        else:
            self.fail("There are no results!")
			
        assert self.is_element_present(By.XPATH, "//div[@title='film1' and @alt='film1']") and \
               len(driver.find_elements_by_xpath("//div[@title='film1' and @alt='film1']")) == count + 1, "The film was not found!"

        #Завершение теста
        driver.quit()
    
    def is_element_present(self, how, what):
        try: self.driver.find_element(by=how, value=what)
        except NoSuchElementException, e: return False
        return True
    
    def is_alert_present(self):
        try: self.driver.switch_to_alert()
        except NoAlertPresentException, e: return False
        return True
    
    def close_alert_and_get_its_text(self):
        try:
            alert = self.driver.switch_to_alert()
            alert_text = alert.text
            if self.accept_next_alert:
                alert.accept()
            else:
                alert.dismiss()
            return alert_text
        finally: self.accept_next_alert = True
    
    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
    unittest.main()
